package com.atguigu.guli.service.trade.service.impl;

import com.atguigu.guli.common.util.OrderNoUtils;
import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.mapper.OrderMapper;
import com.atguigu.guli.service.trade.service.CourseDtoSevice;
import com.atguigu.guli.service.trade.service.MemberDtoService;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    CourseDtoSevice courseDtoSevice;
    @Autowired
    MemberDtoService memberDtoService;
    @Override
    public String createOrder(String memberId, String courseId) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getMemberId,memberId);
        queryWrapper.eq(Order::getCourseId,courseId);
        Order oldOrder = baseMapper.selectOne(queryWrapper);
        if(oldOrder!=null&&oldOrder.getStatus()==1){  //先判断是否有已购买的订单，如果有订单不允许重复创建订单
            throw new GuliException(ResultCodeEnum.ORDER_EXIST_ERROR);
        }


        R courseDtoR = courseDtoSevice.getCourseByCourseId(courseId);
        if(!courseDtoR.getSuccess()){
            throw new GuliException(ResultCodeEnum.UNKNOWN_REASON);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        Object courseDtoObj = courseDtoR.getData().get("courseDto");
        CourseDto courseDto = objectMapper.convertValue(courseDtoObj, CourseDto.class);  //将obj对象变成指定的class类型对象
        R memberDtoR = memberDtoService.getByMemberId(memberId);
        if(!memberDtoR.getSuccess()){
            throw new GuliException(ResultCodeEnum.UNKNOWN_REASON);
        }
        Object memberDtoObj = memberDtoR.getData().get("memberDto");

        MemberDto memberDto = objectMapper.convertValue(memberDtoObj, MemberDto.class);

        Order order =new Order();
        long price = courseDto.getPrice().multiply(new BigDecimal("100")).longValue();  //用BigDecimal解决mysql中的精度问题（单位为分，前端需要转换）
        order.setStatus(0);
        order.setCourseCover(courseDto.getCover());
        order.setCourseId(courseDto.getId());
        order.setCourseTitle(courseDto.getTitle());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(price);
        order.setMemberId(memberDto.getId());
        order.setNickname(memberDto.getNickname());
        order.setMobile(memberDto.getMobile());

        if(oldOrder!=null){  //查看是否有未支付的订单，如果有id，订单号用原来的，改为修改mysql数据库状态
            order.setId(oldOrder.getId());
            order.setOrderNo(oldOrder.getOrderNo());
            baseMapper.updateById(order);
            return order.getId();
        }
        order.setOrderNo(OrderNoUtils.getOrderNo());
        baseMapper.insert(order);
        return order.getId();
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Order::getId,orderNo);
        Order order = baseMapper.selectOne(queryWrapper);
        return order;
    }
}
